package com.ebelter.temperature.activitys;


import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import com.ebelter.btcomlib.bases.activity.BaseActivity;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.utils.KeyBoardShowListener;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.StatusBarUtil;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.temperature.R;
import com.ebelter.temperature.view.TWarringModelDialog;

public class T_SettingActivity extends BaseActivity {

    public static final String TAG="T_SettingActivity";


    TextView topTitleTv;
    TextView setting_warring_ed;
    TextView warring_mode_tv;
    public KeyBoardShowListener keyBoardShowListener = new KeyBoardShowListener(this);
    @Override
    protected int loadLayoutById() {
        return R.layout.activity_t_setting;
    }

    @Override
    protected void initView() {
      //  StatusBarUtil.setColor(this, Color.parseColor("#787ede"));
        FV();
        //监听软键盘的状态
        keyBoardShowListener.setKeyboardListener(new KeyBoardShowListener.OnKeyboardVisibilityListener() {
            @Override
            public void onVisibilityChanged(boolean visible) {
                if (!visible){
                    //软键盘未弹出

                    String c=setting_warring_ed.getText().toString();
                    if (TextUtils.isEmpty(c)){
                        return;
                    }
                    //Intent.ACTION_LOCALE_CHANGED

                    c = c.replace("≥","");

                    float value= NumUtils.string2Float(c);
                    value=NumUtils.numBaoLiuWei1(value);

                    if (value<32||value>42){
                        ToastUtil.show("请输入合理的值范围（32.0℃～42.0℃）");
                        return;
                    }
                    UserSpUtil.writeFloat(Constants.IUser.T_Warring_Value,value);
                    disPlayTWarringValue();
                }
            }
        }, T_SettingActivity.this);
    }

    private void FV() {
        topTitleTv = findViewById(R.id.top_title_tv);
        setting_warring_ed = findViewById(R.id.setting_warring_ed);
        warring_mode_tv = findViewById(R.id.warring_mode_tv);

    }

    @Override
    protected void initData() {
        topTitleTv.setText(getString(R.string.set));

        disPlayTWarringValue();

        disPlayTWarringModel();

    }

    //显示警告的温度值
    @SuppressLint("SetTextI18n")
    private void disPlayTWarringValue(){
        float w=UserSpUtil.readFloat(Constants.IUser.T_Warring_Value,38);
        setting_warring_ed.setText("≥"+w);
    }


    private void disPlayTWarringModel(){
        int warringModel = UserSpUtil.readInt(Constants.IUser.T_Warring_Model,0);
        //0代表震动 1代表响铃

        if (warringModel==0){
            warring_mode_tv.setText("震动模式");
        }else {
            warring_mode_tv.setText("响铃模式");
        }


    }


//    @Override
//    public boolean isSetTranslucent() {
//        return false;
//    }

    //返回键被按下
    public void backClick(View view){
        LogUtils.i(TAG,"返回键被按下");
        finish();
    }

    //高温警告
    public void hightwarring_rl_Click(View view){
        LogUtils.i(TAG,"hightwarring_rl_Click");
        //startActivity(T_DeviceManagerActivity.class);

    }

    //警告模式
    public void warringmode_rl_Click(View view){
        LogUtils.i(TAG,"warringmode_rl_Click");
        //startActivity(T_DeviceManagerActivity.class);

    }

    //设备管理器
    public void deviceManagerClick(View view){
        LogUtils.i(TAG,"deviceManagerClick");
        startActivity(T_DeviceManagerActivity.class);

    }
    //账户信息
    public void accountInformationClick(View view){
        LogUtils.i(TAG,"accountInformationClick");
        startActivity(T_AcountInfoActivity.class);

    }


    //软件版本
    public void softVersionClick(View view){
        LogUtils.i(TAG,"softVersionClick");

      //  UserSpUtil.removeAll();
        startActivity(T_SoftVersionActivity.class);
      //  ActivityManagers.getInstance().closeActivity(BPM_AboutActivity.TAG+":"+BPMMainActivity.TAG);
       // finish();

    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //更新一下名字的意思
       // if (resultCode==resultCodeSuccess&&requestCode==BPM_ModifyNameActivity.BPM_ModifyNameActivity_REQUEST_CODE){
        //    nickname_tv.setText(UserSpUtil.readString(Constants.IUser.NICK_NAME));
      //  }


        if (requestCode==T_DeviceManagerActivity.REQUEST_CODE){

            if (resultCode==1){
                //说明移除了绑定的设备
                finish();
            }
        }

    }


    public void warring_model(View view){
        LogUtils.i(TAG,"警告模式");

        TWarringModelDialog warringModelDialog=new TWarringModelDialog(this);
        warringModelDialog.setListener(new TWarringModelDialog.ICancelOkBtListener() {
            @Override
            public void click(Dialog dialogView, int i) {
                dialogView.cancel();
                //0代表震动 1代表响铃
                if (i==1){//震动
                    UserSpUtil.writeInt(Constants.IUser.T_Warring_Model,0);
                    disPlayTWarringModel();
                }else if (i==2){//响铃
                    UserSpUtil.writeInt(Constants.IUser.T_Warring_Model,1);
                    disPlayTWarringModel();
                }



            }
        });
        warringModelDialog.show();

    }


    public void device_managerClick(View view){

        Intent intent=new Intent(this,T_DeviceManagerActivity.class);
        startActivityForResult(intent,T_DeviceManagerActivity.REQUEST_CODE);
    }


}
